package tools.settings;

import java.io.File;
import java.util.*;
import tools.settings.xml.XMLSettingsReader;

/**
 *
 * @author Lynne Vandousselaere
 * TODO add LOGGER stuff here.
 */
public class MapSettings extends HashMap<String, Map<String, String>>
        implements Settings {

    // Name or location of this settings object.
    private ArrayList<File> locations = new ArrayList<File>();

    private void readFromLocation(File location) throws SettingsReaderException {
        SettingsReader reader = new XMLSettingsReader();
        super.putAll(reader.getSettings(location));
    }

    /*
     * implemented methods
     */
    @Override
    public List<File> getLocations() {
        return new ArrayList<File>(locations);
    }

    @Override
    public String getSettingsValue(String sname) {
        sname = sname.toLowerCase();
        if (get(sname) == null)
            return null;
        return get(sname).get("value");
    }

    @Override
    public boolean contains(Collection<String> snames) {
        for (Iterator<String> it = snames.iterator(); it.hasNext();)
            if (!containsKey(it.next().toLowerCase()))
                return false;
        return true;
    }

    @Override
    public boolean contains(String[] snames) {
        for (int i = 0; i < snames.length; i++)
            if (!containsKey(snames[i].toLowerCase()))
                return false;
        return true;
    }

    @Override
    public Map<String, String> getParameters(String sname) {
        return get(sname.toLowerCase());
    }

    @Override
    public List<String> getSettingsByType(String type, boolean dl) {
        String link = dl?"dl" : "ul";
        // TODO: heavy - index this in constructor.
        List<String> list = new ArrayList<String>();

        for (Iterator<String> keys = keySet().iterator(); keys.hasNext();) {
            String sname = keys.next();
            if (type.equalsIgnoreCase(get(sname).get("type")) &&
                    "1".equalsIgnoreCase(get(sname).get(link)))
                list.add(sname);
        }
        return list;
    }

    @Override
    public String getParameterValue(String sname, String pname) {
        sname = sname.toLowerCase();
        if (get(sname) == null)
            return null;
        return get(sname).get(pname);
    }

    @Override
    public void clear() {
        super.clear();
        locations.clear();
    }

    @Override
    public boolean isEmpty() {
        return locations.isEmpty();
    }

    @Override
    public void readSettings(File sfile) throws SettingsReaderException {
        readFromLocation(sfile);
        locations.add(sfile);
    }

    @Override
    public String getSettingsValue(String sname, boolean dl) {
        if(sname.equalsIgnoreCase("rcv_sensitivity") ||
                sname.equalsIgnoreCase("noise_figure")){
            if(dl)
                return getSettingsValue("ms_"+sname);
            else
                return getSettingsValue("bs_"+sname);
        }
        return null;
    }

    

}
